package com.fastloan.mgt.invest.weixin.service.impl;

import com.alibaba.fastjson.JSON;
import com.fastloan.mgt.invest.weixin.core.AccessToken;
import com.fastloan.utils.HttpUtil;
import com.fastloan.mgt.invest.weixin.core.WeixinConstant;
import com.fastloan.mgt.invest.weixin.model.menu.Button;
import com.fastloan.mgt.invest.weixin.model.menu.Menu;
import com.fastloan.mgt.invest.weixin.model.menu.SubButton;
import com.fastloan.mgt.invest.weixin.service.InitMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author rankai
 *         createTime 2017-08-2017/8/17 14:10
 */
@Service
public class InitMenuServiceImpl implements InitMenuService {

    @Override
    public void initMenu() {
        List<Button> list = new ArrayList<>();

        Button button = new Button();
        button.setName("乾康产品");
        List<SubButton> subButtonsOne = new ArrayList<>();
        SubButton subButtonOne = new SubButton();
        subButtonOne.setName("乾康快贷");
        subButtonOne.setType("click");
        subButtonOne.setKey("1_1");
        subButtonsOne.add(subButtonOne);
        SubButton subButtonOne2 = new SubButton();
        subButtonOne2.setName("乾康快信");
        subButtonOne2.setType("click");
        subButtonOne2.setKey("1_2");
        subButtonsOne.add(subButtonOne2);
        SubButton subButtonOne3 = new SubButton();
        subButtonOne3.setName("乾康快接");
        subButtonOne3.setType("click");
        subButtonOne3.setKey("1_3");
        subButtonsOne.add(subButtonOne3);
        SubButton subButtonOne4 = new SubButton();
        subButtonOne4.setName("乾康易数");
        subButtonOne4.setType("click");
        subButtonOne4.setKey("1_4");
        subButtonsOne.add(subButtonOne4);
        button.setSubButtons(subButtonsOne);
        list.add(button);

        Button button2 = new Button();
        button2.setName("关于乾康");
        List<SubButton> subButtons = new ArrayList<>();
        SubButton subButton = new SubButton();
        subButton.setName("点赞乾康");
        subButton.setType("click");
        subButton.setKey("2_1");
        subButtons.add(subButton);
        SubButton subButton2 = new SubButton();
        subButton2.setName("关于乾康");
        subButton2.setType("click");
        subButton2.setKey("2_2");
        subButtons.add(subButton2);
        button2.setSubButtons(subButtons);
        list.add(button2);

        Menu menu = new Menu();
        menu.setList(list);
        String string = JSON.toJSONString(menu);
        System.out.println(string);
        String accessToken = AccessToken.getAccessToken();
        if (StringUtils.isBlank(accessToken)) {
            System.out.println("更新菜单失败");
        } else {
            String menuUrl = WeixinConstant.WEIXIN_MENU_API + accessToken;
            HttpUtil.post(menuUrl, string);
        }
    }
}
